package com.aj.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.AccountDao;
import com.aj.databases.entry.Account;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.UpdatePasswordRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.http.response.UpdatePasswordResponse;

public class UpdatePassword extends BaseActivity {
	private Button ok;
	private EditText password_enter;
	private EditText repassword_enter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_password);
		initView();
	}

	private void initView() {
		ok = (Button) findViewById(R.id.ok);
		password_enter = (EditText) findViewById(R.id.password_enter);
		repassword_enter = (EditText) findViewById(R.id.repassword_enter);
		setActionBarTitle(R.string.setpassword_title_text);
		ClickEvent clickEvent = new ClickEvent();
		ok.setOnClickListener(clickEvent);
	}

	class ClickEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.ok:
				if (password_enter.getText().toString().trim().length() == 0) {
					Toast.makeText(UpdatePassword.this,
							getString(R.string.enter_password_hinttext),
							Toast.LENGTH_LONG).show();
					return;
				}
				if (repassword_enter.getText().toString().trim().length() == 0) {
					Toast.makeText(UpdatePassword.this,
							getString(R.string.enter_sure_password_hinttext),
							Toast.LENGTH_LONG).show();
					return;
				}
				if (!repassword_enter.getText().toString().trim()
						.equals(password_enter.getText().toString().trim())) {
					Toast.makeText(UpdatePassword.this,
							getString(R.string.double_sure_password_hinttext),
							Toast.LENGTH_LONG).show();
					return;
				}
				UpdatePasswordRequest updatePasswordRequest = new UpdatePasswordRequest();
				if (LoveHomeApplication.token != null) {
					final AccountDao accountDao = new AccountDao(
							UpdatePassword.this);
					// UserDao userDao = new UserDao(UpdatePassword.this);
					final Account account = accountDao
							.getByToken(LoveHomeApplication.token);
					if (account != null) {
						updatePasswordRequest.excute(UpdatePassword.this,
								account.getLast_used_user_id(), account
										.getPassword(), password_enter
										.getText().toString(),
								new IHttpCallBack() {
									@Override
									public void success(String result) {
										try {
											UpdatePasswordResponse updatePasswordResponse = new UpdatePasswordResponse(
													new JSONObject(result));
											Common common = updatePasswordResponse
													.getCommon();
											if (common != null
													&& common.getReturnCode() != null
													&& common
															.getReturnCode()
															.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
												Toast.makeText(
														UpdatePassword.this,
														getString(R.string.update_password_success_text),
														Toast.LENGTH_LONG)
														.show();
												if (SafeActivity.intances != null) {
													SafeActivity.intances
															.finish();
													SafeActivity.intances = null;
												}
												LoveHomeApplication.token = "";
												accountDao.del(account);
											} else {
												Toast.makeText(
														UpdatePassword.this,
														common.getErrorMsg(),
														Toast.LENGTH_LONG)
														.show();
											}
										} catch (JSONException e) {
											e.printStackTrace();
										}
										finish();
									}

									@Override
									public void fail(String result) {
										try {
											UpdatePasswordResponse updatePasswordResponse = new UpdatePasswordResponse(
													new JSONObject(result));
											Common common = updatePasswordResponse
													.getCommon();
											if (common != null) {
												Toast.makeText(
														UpdatePassword.this,
														common.getErrorMsg(),
														Toast.LENGTH_LONG)
														.show();
											}
										} catch (JSONException e) {
											e.printStackTrace();
										}
										finish();
									}
								});
					}
				}
				break;
			default:
				break;
			}
		}
	}
}
